package com.gdupt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gdupt.entity.SalesOpportunity;
import com.gdupt.entity.ServiceUserAllot;
import com.gdupt.entity.vo.ServiceUserAllotAndServiceManagementVo;
import com.gdupt.service.IServiceManagementService;
import com.gdupt.service.IServiceUserAllotService;
import com.gdupt.until.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 服务用户分配表 服务用户关系表 前端控制器
 * </p>
 *
 * @author gdupt
 * @since 2018-11-21
 */
@RestController
@RequestMapping("/service-user-allot")
public class ServiceUserAllotController {

    @Autowired
    private IServiceUserAllotService userAllotService;
    @Autowired
    private IServiceManagementService serviceManagementService;
    private  boolean flg=false;
//服务分配
    @RequestMapping("/allotService")
    public ResponseData allotService(ServiceUserAllot serviceUserAllot){
        if (serviceUserAllot.getUserName()!=null&&serviceUserAllot.getUserName()!="") {
             flg = userAllotService.insertAllotService(serviceUserAllot);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("status","已分配");
        map.put("id",serviceUserAllot.getServiceId());
        boolean log=false;
        if (flg) {
            log = serviceManagementService.updateStatus(map);
        }
        if (log){
            return ResponseData.okMessage("分配成功！");
        }
        else { return ResponseData.fail("分配失败！");}

    }
 //    查找‘已分配’的服务
    @RequestMapping("/handleService")
    public  ResponseData handleService(Page page,@RequestParam(value = "customerName",required = false) String customerName,
                                       @RequestParam(value = "outline",required = false) String outline,@RequestParam(value = "serviceType",required = false) String serviceType){
        List<ServiceUserAllotAndServiceManagementVo> list = userAllotService.findHandleService(page,customerName,outline,serviceType);
        return  ResponseData.okPage(page.getTotal(),list);
    }
//    服务处理
    @PostMapping("/setHandleService")
    public ResponseData setHandleService(ServiceUserAllot serviceUserAllot){
        if (serviceUserAllot.getServiceHandle()!=null&&serviceUserAllot.getServiceHandle()!=""
              &&serviceUserAllot.getHandleName()!=null&&serviceUserAllot.getHandleName()!=""
                &&serviceUserAllot.getHandleTime()!=null&&serviceUserAllot.getHandleTime()!="")
        {
            flg = userAllotService.updateHandleService(serviceUserAllot);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("status","已处理");
        map.put("id",serviceUserAllot.getServiceId());
        boolean log=false;
        if (flg) {
            log = serviceManagementService.updateStatus(map);
        }
        if (log){
            return ResponseData.okMessage("处理完毕！");
        }
        else { return ResponseData.fail("提交失败,检查内容是否填写完整！");}

    }
//    返回服务反馈要处理的数据
    @RequestMapping("/feedbackService")
    public  ResponseData feedbackService(Page page,@RequestParam(value = "customerName",required = false) String customerName,
                                         @RequestParam(value = "outline",required = false) String outline,@RequestParam(value = "serviceType",required = false) String serviceType){
        List<ServiceUserAllotAndServiceManagementVo> list = userAllotService.findFeedbackService(page,customerName,outline,serviceType);
        return  ResponseData.okPage(page.getTotal(),list);
    }
//    服务反馈
    @RequestMapping("/setFeedbackService")
    public ResponseData setFeedbackService(ServiceUserAllot serviceUserAllot) {
        boolean log = false;
        Map<String, Object> map = new HashMap<>();
        if (serviceUserAllot.getSatisfaction() >= 3 && serviceUserAllot.getHandleResult() != null && serviceUserAllot.getHandleResult() != "") {
            flg = userAllotService.updateFeedbackService(serviceUserAllot);
            map.put("status", "已归档");
            map.put("id", serviceUserAllot.getServiceId());
            if (flg) {
                log = serviceManagementService.updateStatus(map);
            }
            if (log) {
                return ResponseData.okMessage("归档成功！");
            } else {
                return ResponseData.fail("归档失败！");
            }
        } else if (serviceUserAllot.getSatisfaction()!=0&&serviceUserAllot.getHandleResult() != null && serviceUserAllot.getHandleResult() != "") {
            map.put("status", "已分配");
            map.put("id", serviceUserAllot.getServiceId());
            log = serviceManagementService.updateStatus(map);
            if (log) {
                return ResponseData.okMessage("该服务需重新处理！");
            } else {
                return ResponseData.fail("操作失败！");
            }
        }
        return  ResponseData.fail("操作失败，请检查内容是否填写完整？");
    }
//    服务归档
    @RequestMapping("/archiveService")
    public ResponseData archiveService(Page page,@RequestParam(value = "customerName",required = false) String customerName,
                                       @RequestParam(value = "outline",required = false) String outline,@RequestParam(value = "serviceType",required = false) String serviceType) {
        List<ServiceUserAllotAndServiceManagementVo> list = userAllotService.findArchiveService(page,customerName,outline,serviceType);
        return  ResponseData.okPage(page.getTotal(),list);
    }

}
